package io.zt.dangyuan;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import io.zt.dangyuan.object.AddressBook;
import io.zt.dangyuan.object.AddressList;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import io.zt.dangyuan.util.Cons;

public class AddressListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<AddressList.ListEntity> datas;
    private List<AddressBook.ListEntity> bookDatas;
    private Context context;

    private AddressList book;

    public void setBook(AddressList book) {
        this.book = book;
    }

    public AddressListAdapter(Context context, List<AddressList.ListEntity> datas, List<AddressBook.ListEntity> bookDatas, AddressList book) {
        super();
        this.context = context;
        this.datas = datas;
        this.bookDatas = bookDatas;
        this.mInflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub

        int count = 1;

        if (bookDatas.size() != 0) {
            count++;
        }

        return datas.size() + bookDatas.size() + count;


    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ViewHolder2 holder2 = null;
        ViewHolder3 holder3 = null;

//		if (convertView == null) {
//			holder=new ViewHolder();
//			convertView = mInflater.inflate(R.layout.address_list_item, null);
//			holder.tv_itemName = (TextView) convertView.findViewById(R.id.address_list_item_name);
//
//			convertView.setTag(holder);
//		}else {
//			holder = (ViewHolder)convertView.getTag();
//		}
//
//
//
//
//		if (holder3 == null) {
//			holder3= new ViewHolder3();
//			convertView = mInflater.inflate(R.layout.address_book_item, null);
//			holder3.tv_itemName = (TextView) convertView.findViewById(R.id.addres_book_name);
//			holder3.tv_itemphoen = (TextView) convertView.findViewById(R.id.addres_book_phone);
//			holder3.iv_image = (ImageView) convertView.findViewById(R.id.addres_book_image);
//
//			convertView.setTag(holder);
//		}else {
//			holder3 = (ViewHolder3)convertView.getTag();
//		}


        if (0 == position) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.address_list_item2, null);
            holder.tv_itemName = (TextView) convertView.findViewById(R.id.address_list_item2_name);

//			convertView.setTag(holder);

            if (book != null) {
                holder.tv_itemName.setText(book.getDepname());
            }

        } else if (datas.size() + 1 == position && bookDatas.size() != 0) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.address_list_item2, null);
            holder.tv_itemName = (TextView) convertView.findViewById(R.id.address_list_item2_name);

//			convertView.setTag(holder);
            holder.tv_itemName.setText("部门人员");

        } else if (datas.size() + 1 > position) {
//			if (convertView == null || position == 1 ) {
            holder2 = new ViewHolder2();
            convertView = mInflater.inflate(R.layout.address_list_item, null);
            holder2.tv_itemName = (TextView) convertView.findViewById(R.id.address_list_item_name);

//				convertView.setTag(holder);
//			}else {
//				holder2 = (ViewHolder2)convertView.getTag();
//			}

            AddressList.ListEntity object = datas.get(position - 1);
            holder2.tv_itemName.setText(object.getGroupname());

        } else if (datas.size() + 1 < position) {
//			if (convertView == null || position == datas.size() + 2) {

            holder3 = new ViewHolder3();
            convertView = mInflater.inflate(R.layout.address_book_item, null);
            holder3.tv_itemName = (TextView) convertView.findViewById(R.id.addres_book_name);
            holder3.tv_itemphoen = (TextView) convertView.findViewById(R.id.addres_book_phone);
            holder3.iv_image = (CircleImageView) convertView.findViewById(R.id.addres_book_image2);
            holder3.tv_work_uint = (TextView) convertView.findViewById(R.id.tv_work_uint);
            holder3.iv_image.setBorderColor(Color.WHITE);
            holder3.iv_image.setBorderWidth(4);

//				convertView.setTag(holder3);
//			}else {
//				holder3 = (ViewHolder3)convertView.getTag();
//			}
            AddressBook.ListEntity object = bookDatas.get(position - datas.size() - 2);
            holder3.tv_itemName.setText(object.getRealname());
            holder3.tv_itemphoen.setText(object.getMobile());
            holder3.tv_work_uint.setText(object.getWorkunit());
//			Glide.with(context)
//					.load( Cons.BASE_URL + object.getUserimg())
//					.into(holder3.iv_image);

            Glide.with(context).load(Cons.BASE_URL + object.getUserimg()).placeholder(R.drawable.ic_laucher).into(holder3.iv_image);

        }


        return convertView;
    }

    class ViewHolder {
        TextView tv_itemName;
    }

    class ViewHolder2 {
        TextView tv_itemName;
    }

    class ViewHolder3 {
        TextView tv_itemName;
        TextView tv_itemphoen;
        CircleImageView iv_image;
        TextView tv_work_uint;
    }


}